﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace RPCServer.Struct
{
    class SyncPool<Item>
    {
        readonly ConcurrentStack<Item> _items = new();
        Func<Item> _creater;

        public SyncPool(Func<Item> creater)
        {
            _creater = creater;
        }

        public bool TryTake(out Item item)
        {
            if (_items.IsEmpty)
            {
                item = _creater();
                _items.Push(item);
            }
            return _items.TryPop(out item);
        }

        public void Recycle(Item t)
        {
            _items.Push(t);
        }

    }
}
